package com.enjoy.book.action;

import com.enjoy.book.bean.User;
import com.enjoy.book.biz.UserBiz;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user.let")
/**
 * @author 徐佳澎
 */
public class UserServlet extends HttpServlet {
    UserBiz userBiz =new UserBiz();
    public static void main(String[] args) {
        UserBiz userBiz =new UserBiz();
        System.out.println(userBiz.getUser("super","123"));
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    /**
     * /user.let?type = login 登陆 /:代表项目的根目录--代表web文件夹,前端资源;
     * /user.let?type = exit 退出
     *  web
     *  |-login.html
     *  |-user.let?type=login 特殊的java代码,通过虚拟路径将其配置成网络能访问的资源;
     *  |-index.jsp; 同一级别
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       HttpSession session = req.getSession();
        /**
         * 设置编码,预防请求中的乱码;
         */
        req.setCharacterEncoding("utf-8");
       resp.setContentType("text/html;charset=utf-8");
        /**
         * 输出流对象,帮助我们打印信息
         */
        PrintWriter out =resp.getWriter();
        /**
         * 判断用户的请求类型为login
         */
        String  method =req.getParameter("type");
        switch (method)
        {
            case "login":
                String passcode=session.getAttribute("code").toString();
                String  name =req.getParameter("name");
                String pwd =req.getParameter("pwd");
                String valcode= req.getParameter("valcode");
                User user =userBiz.getUser(name,pwd);
                if(user==null){
                out.println("<script>alert('用户名或密码不正确');location.href='login1.html'</script>");
                }else  if(!valcode.equalsIgnoreCase(passcode)){
                    out.println("<script>alert('验证码不正确');location.href='login1.html'</script>");
                }
                else{
                    session.setAttribute("user",user);
                out.println("<script>alert('登陆成功');location.href='index.jsp'</script>");
                }
                break;
            case "exit":
                session.invalidate();
                out.println("<script>alert('退出成功');parent.window.location.href='login1.html'</script>");
                break;
            case "modifyPwd":
                String newpwd =req.getParameter("newpwd");
                User modifyUser =(User) session.getAttribute("user");
                long id =modifyUser.getId();
                int count = userBiz.modifyPwd(id,newpwd);
                if(count >0){
                    out.println("<script>alert('修改成功,请重新登录!');parent.window.location.href='login1.html'</script>");
                }else{
                    out.println("<script>alert('修改失败');</script>");
                }
                break;
            default:
        }
    }
}
